﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

<div id="result"></div>
<% Html.Telerik().Grid<WiseLink.RDS.Model.DeviceViewModel>()
    .Name("DeviceGrid")
     .ToolBar(toolBar => toolBar.Template(() =>
     {
         %>
                     <label>Input Device Serial#:</label>
                      <%= Html.TextBox("deviceSerialNoTxt")%>
                     <button id="btnAddDevice" onclick="onDeviceAction(this)">Add</button>
                     <label>Customer:</label>
                     <% Html.Telerik().DropDownList()
                            .Name("Customers")
                            .DataBinding(binding =>binding.Ajax().Select("GetCustomerName", "Customer"))
                            .DropDownHtmlAttributes(new {style =  "margin :2,5,2,0"})
                            .Render();
                      %>
                      <button id="btnSaleDevice" onclick="onDeviceAction(this)">Sell</button>
                      <button id="btnInStock" onclick="onDeviceAction(this)">InStock</button>
                      <button id="btnRegister" onclick="onDeviceAction(this)">Un/Register</button>
     
         <%
      }))
    .Columns(columns =>
    {
        //columns.Bound(o => o.DeviceID).ClientTemplate("<input type='checkbox' name='checkedDevice' value='<#= DeviceID #>' />").Title("Check").Width(60).HtmlAttributes(new { style = "text-align:center" });
        columns.Bound(o => o.DeviceID).Width(100).Hidden();
        columns.Bound(o => o.CustomerNo).Width(100);
        columns.Bound(o => o.CustomerName).Width(100);
        columns.Bound(o => o.VehicleLicensePlate).Width(80).Title("License");
        columns.Bound(o => o.ChassiNo).Width(150);
        columns.Bound(o => o.DeviceSerialNo).Width(150);
        columns.Bound(o => o.IDCNo).Width(150);
        columns.Bound(o => o.DevicePurchaseDate).Format("{0:MM/dd/yyyy}").Width(150);
        columns.Bound(o => o.IsRegistered).Width(100).Title("Register")
            .ClientTemplate("<input type='checkbox' disabled='disabled' name='IsRegistered' <#= IsRegistered? checked='checked' : '' #> />");
        columns.Bound(o => o.IsSpecialIDC).Width(100).Title("SpecialIDC")
            .ClientTemplate("<input type='checkbox' disabled='disabled' name='IsSpecialIDC' <#= IsSpecialIDC? checked='checked' : '' #> />");
    })
    .DataBinding(dataBinding => dataBinding.Ajax().Select("FirstLook", "Device"))
    .Pageable()
    .Sortable(x=>x.OrderBy( sort=>sort.Add(d=>d.DeviceID).Descending()))
    .Selectable()
    .Scrollable()
    .ClientEvents(events => events.OnRowSelect("onRowSelected"))
    .Filterable().Render();
    
    %>
 
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="HeadContent" runat="server">
<script type="text/javascript">
    var deviceid = 0;
    
    function onDeviceAction(e) {
        
        if (e.id != "btnAddDevice" && deviceid == 0) {
            alert("Please select a device first");
            return;
        }

        if (e.id == "btnAddDevice") {

            $.post("Device/AddDevice",
                    "deviceSerialNo=" + $("#deviceSerialNoTxt")[0].value,
                    "json")
            .success(DeviceAjaxSucceed)
            .error(function () { alert("error"); })


        } else if (e.id == "btnSaleDevice") {

            var customer = $("#Customers").data("tDropDownList");
            $.post("Device/SellDevice",
                    "customerid=" + customer.value() + "&deviceid=" + deviceid,
                    "json")
            .success(DeviceAjaxSucceed)
            .error(function () { alert("error"); })
            } else if (e.id == "btnRegister") {

                $.post("Device/RegisterDevice",
                    "deviceid=" + deviceid,
                    "json")
            .success(DeviceAjaxSucceed)
            .error(function () { alert("error"); })
            } else if (e.id == "btnInStock") {

                $.post("Device/InStockDevice",
                    "deviceid=" + deviceid,
                    "json")
            .success(DeviceAjaxSucceed)
            .error(function () { alert("error"); })
            }

    }
    function onRowSelected(e) {
        deviceid = e.row.cells[0].innerHTML;
        ReplaceContentInContainer("result", "Selected DeviceID=" + deviceid);
    }


    function ReplaceContentInContainer(id, content) {
        var container = document.getElementById(id);
        container.innerHTML = content;
    }
    function DeviceAjaxSucceed(data) {
        if (data.toLowerCase().indexOf("succeed") > 0) {
            var customer = $("#Customers").data("tDropDownList");
            var deviceGrid = $('#DeviceGrid').data('tGrid');
            deviceGrid.ajaxRequest();
            customer.reload();
        } else {
            ReplaceContentInContainer("result", data);
        };
    }


</script>    

</asp:Content>
